<template>
	<view>
		<!-- 搜索区域 -->
		<view class="inputs">
			<uni-search-bar  @input="input" :radius="100" :cancelButton="none" ></uni-search-bar>
		</view>
		<!-- 搜索建议区域 -->
		<view class="s1" v-if="searchResults.length !==0">
			<view class="s2" v-for="(item,i) in searchResults" :key="i" @click="gets1(item)">
				<view class="s3">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史区域 -->
		<view class="Boxls" v-else>
			<!-- 标题区域 -->
			<view class="litou">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="20" @click="getqk"></uni-icons>
			</view>
			<!-- 内容区域 -->
			<view class="litext">
				<uni-tag  v-for="(item2,index) in historys" :key="i" :text="item2" inverted="true" @click="gettag(item2)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 延时器timer
				timer:null,
				kw:'',
				// 搜索结果列表
				searchResults:[],
				// 搜索历史数组
				histroyList:['a','ab','abc']
			};
		},
		methods:{
			input(input){
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.kw = input
					console.log(this.kw);
					this.getSearchList()
				},500)
			},
			// 搜索建议的网络请求
			async getSearchList(){
				if(this.kw === '') {
					this.searchResults = []
					return
				}
				//发起请求
				const {data:s}=await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if(s.meta.status !==200) return uni.$showMsg()
				this.searchResults = s.message
				console.log(this.searchResults);
				 this.saveSearchHistory()
			},
			// 保存搜索关键词的方法
			saveSearchHistory(){
				this.histroyList.push(this.kw)
				const set = new Set(this.histroyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.histroyList = Array.from(set)
				uni.setStorageSync('kw',JSON.stringify(this.histroyList))
			},
			// 建议跳转商品页面
			gets1(item){
				console.log(item.goods_id);
				uni.navigateTo({
					url:'../goods_detail/goods_detail?goods_id='+item.goods_id
				})
			},
			// 清空数据的点击事件
			getqk(){
				// this.kw = [];
				this.histroyList = []
				uni.setStorageSync('kw', '[]')
			},
			// 历史记录的跳转点击事件
			gettag(item){
				console.log(item);
				uni.navigateTo({
					url:'../goods_list/goods_list?query='+item
				})
			}
		},
		// 计算属性
		computed:{
			historys(){
				return [...this.histroyList].reverse()
			}
		},
		// 初始化生命周期
		onLoad() {
			this.histroyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
	.s1{
		padding: 0 5px;
		.s2{
			display: flex;
			justify-content: space-between;
			margin: 20px 0;
			border-bottom: 1px solid #fefefe;
			.s3{
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				font-size: 13px;
			}
		}
	}
	.inputs{
		position: sticky;
		top: 0px;
		z-index: 999;
	}
	.Boxls{
		padding: 3px 5px;
		.litou{
			display: flex;
			justify-content: space-between;
			height: 40px;
			line-height: 40px;
			align-items: center;
			border-bottom: 1px solid #fefefe;
			
			
		}
		.litext{
			display: flex;
			flex-wrap: wrap;
			
		}
		.uni-tag{
			margin-top: 5px;
			margin-right: 7px;
			height: 60px;
		}
	}
</style>
